



/*This program is for identifying couples who meet the criteria for
inclusion in the sample definitions for married/cohabiting couples at 
risk for first or second birth.
*/

*version 12
clear
set more off

* directory
local indir "../replication-package"
local linked "data/setup/03_linked_women.dta" /* identify input data file, e.g. racebridge.dta, e.g. linkedcpl_beta.dta */ 
local sampled "data/setup/04_sampled_women.dta" /* identify output data file, e.g. racebridge.dta, e.g. linkedcpl_beta.dta */ 

cd "`indir'"

use "`linked'", clear

/******************
*******************
NEXT STEP: Create first birth and second birth variables.
*******************
******************/

* note, eldch maxes at age 22 before 1989; should not affect code here

	
* NIU (99) for eldch means no children present in the HH

/*  
gen eldch4 = eldch4m
  /* set NIU codes to missing */
  replace eldch4 =. if eldch4 == 99

gen eldch8 = eldch8m
  replace eldch8 =. if eldch8 == 99
*/  

* could rename nchild_cohab0 variables to drop the cohab, given new sploc pointers  
  * nchild4 is number of children in household at 4m, nchild_cohab04m are number of infants in the household
  
/*First, create variable for a first child to male partner. 
This uses nchild_cohab0mm to show couples who go from no 
children at all to at least one child under age 1. The 
couples DO NOT already have other children.*/
/* Note, before 1982 approx half of couples flagged as having a new child have 2+ children in the HH at W5, so setting these to missing */
/*MIS4 to MIS5*/


* identify women eligible to have a first birth

	gen birthsample=0
		replace birthsample=1 if agef4<45 & nchildf4==0 & matchf_mis48==3

	label var birthsample "No children, women <45, linked MIS 4 & 8"  
	label define birthsample 0 "0 - Not in universe" 1 "1-In first birth universe", replace
	label values birthsample birthsample

	
	* Code changes in number of children in the household
gen childtransf_mis48 = -9
	* no children either wave
	replace childtransf_mis48 = 1 if nchildf4 == 0 & nchildf8 == 0 
	* already had child in MIS 4
	replace childtransf_mis48 = 2 if nchildf4 > 0
	* no children at MIS 4, oldest child is age 0 at MIS 8
	replace childtransf_mis48 = 3 if nchildf4 == 0 & nchildf8 > 0 & eldchf8 == 0
	* no children at MIS 4, oldest child is age 1 at MIS 8
	replace childtransf_mis48 = 4 if nchildf4 == 0 & nchildf8 > 0 & eldchf8 == 1
	* no children at MIS 4, oldest child is age 2+ at MIS 8
	replace childtransf_mis48 = 5 if nchildf4 == 0 & nchildf8 > 0 & eldchf8 >= 2 
	* not in sample
	replace childtransf_mis48 = 0 if birthsample == 0

	label define childtransf 0 "0: NIU first births" 1 "1: No children MIS 4 & 8" 2 "2: 1+ children in MIS4"  3 "3: Childless MIS4, eldest child is age 0 at MIS8" ///
			4 "4: Childless MIS4, eldest child is age 1 at MIS8" 5 "5: Childless MIS4, eldest child is age 2+ at MIS8", replace

	label var childtransf_mis48 "Transitions in children present, MIS 4-8 (linked female sample)"
	label val childtransf_mis48 childtransf

	
	
	*replace childtransf_mis48 = . if matchf_mis48 == 1
	
	tab childtransf_mis48, missing
	
	tab childtransf_mis48 matchf_mis48 , missing

	tab childtransf_mis48 if agef4 < 45 & matchf_mis48 > 1, missing

	
	gen unionstatf4 = .
		replace unionstatf4 = 1 if inunionf4 == 1 & samesexf4 == 0 & cohabf4==0
		replace unionstatf4 = 2 if inunionf4 == 1 & samesexf4 == 0 & cohabf4==1
		replace unionstatf4 = 3 if inunionf4 == 1 & samesexf4 == 1 
		replace unionstatf4 = 4 if inunionf4 == 0
		
	label variable unionstatf4 "Union status at MIS4"
	label define unionstat 1 "1: Married, different-sex partner" 2 "2: Cohabiting, different-sex partner" ///
		3 "3: Same-sex partner" 4 "4: Unpartnered"
	label values unionstatf4 unionstat
	
	tab unionstatf4, missing
	tab marstf4 unionstatf4, missing
	tab inunionf4 unionstatf, missing
	tab cohabf4 unionstatf, missing
	
	
	tab childtransf_mis48 if birthsample==1 & unionstat == 1, missing
	tab childtransf_mis48 if birthsample==1 & unionstat == 2, missing
	tab childtransf_mis48 if birthsample==1 & unionstat == 3, missing
	tab childtransf_mis48 if birthsample==1 & unionstat == 4, missing

	

gen married=0
replace married=1 if cohabf4==0 & cohabf8==0 & inunionf4 == 1 & inunionf8 == 1
tab married



gen cohab=0
replace cohab=1 if cohabf4==1 & cohabf8==1 &  inunionf4 == 1 & inunionf8 == 1
tab cohab

gen cohabtomarr=0
replace cohabtomarr=1 if cohabf4==1 & cohabf8==0 &  inunionf4 == 1 & inunionf8 == 1
tab cohabtomarr

label var cohab "Couple married at both MIS4 & MIS8"
label var cohab "Couple cohabiting at both MIS4 & MIS8"
label var cohabtomarr "Couple cohabiting at MIS4 and married at MIS8"


save "`sampled'", replace






